<?php

class SiteController extends Controller {

    public function actionIndex() {
        if (isset(Yii::app()->session['_lang'])) {
            $language = Yii::app()->session['_lang'];
        } else {
            Yii::app()->session['_lang'] = 'vi';
        }
        if (Yii::app()->session['_bookedRoom']) {
            unset(Yii::app()->session['_bookedRoom']);
        }
        $hotel_conditions = new EMongoCriteria;
        $hotel_conditions->active = true;
        $hotel_conditions->sort('display_order', EMongoCriteria::SORT_ASC);
        $hotel = Hotel::model()->findAll($hotel_conditions);
        $exchangeRates = MyFunction::convert_currency();
        $params = array();
        $lang = 'en';
        if (isset($_POST['_lang'])) {
            $lang = $_POST['_lang'];
        }
        foreach ($hotel as $h) {
            //if ($h->name !== 'silverland-sakyo-hotel-spa') {
           
            
            $default_currency = strtoupper($h->default_currency);
            $exchangeVND = $exchangeRates['rates'][$default_currency]['sell'];
            $fromDate = date('Y-m-d');
            $toDate = date('Y-m-d', strtotime("$fromDate +1 day"));
             /*getRoom type*/
            $roomtype_condition = new EMongoCriteria;
            $roomtype_condition->hotel = $h['name'];
            $roomtype_condition->sort('order', EMongoCriteria::SORT_DESC);
            $roomtype = Roomtype::model()->findAll($roomtype_condition);
            /*get rate*/
            $conditions = new EMongoCriteria;
            $conditions->date = array(
                '$gte' => MyFunction::getDate($fromDate),
                '$lt' => MyFunction::getDate($toDate)
            );
            $conditions->hotel = $h->name;
            //$conditions->singleNet = array('$gt' => 1);
            //$conditions->limit(1);
            $conditions->roomType = $roomtype[0]['roomType'];
            $rate = Rate::model()->find($conditions);
            
            $minRateToday = 0;
            $promotion_condition = new EMongoCriteria;
            $promotion_condition->hotel = $h['name'];
            $promotion_condition->nightFrom = 1;
            $promotion_condition->checkInToDate = array(
                '$gte' => MyFunction::getDate($toDate)
            );
            $promotion_condition->checkInFromDate = array(
                '$lte' => MyFunction::getDate($fromDate)
            );
            $promotion_condition->sort('amount_OR_percentage', EMongoCriteria::SORT_DESC);
            $promotion = Promotion::model()->findAll($promotion_condition);
            $discount = array();


            if (isset($promotion) && count($promotion)) {
                foreach ($promotion as $r) {
                    $discount[] = $r['amount_OR_percentage'];
                }
                arsort($discount);
                $minRateToday = ($rate['singleNet'] * (100 - $discount[0]) / 100);
            }
            else{
                $minRateToday = ($rate['singleNet']);
            }

            $params[$h->name]['hotel'] = $h->name;
            $params[$h->name]['area'] = $h->area[$lang];
            $params[$h->name]['hotelName'] = $h->fullName;
            $params[$h->name]['star'] = $h->graded_stars;
            $params[$h->name]['singleNet'] = $minRateToday;
            $params[$h->name]['logo'] = $h->logo;
            //$params['cover'] = $h->cover;
            //$params['slide'] = $h->slide;
            $params[$h->name]['address'] = $h->address[$lang] . ', ' . $h->city[$lang] . ', ' . $h->country[$lang];
            //$params['short_description'] = $params['short_description'][$lang];
            //}
        }
        $this->render('index', array('hotel' => $params));
    }

}